package dao;

import hibernateUtils.HibernateUtil;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import beans.AreaBean;

public class HibernateAreaDAO {
	private static HibernateAreaDAO instancia;
	private static SessionFactory sf;

	public static HibernateAreaDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateAreaDAO();
		}
		return instancia;
	}

	public AreaBean buscarArea(AreaBean ab) {
		Session session = sf.openSession();
		AreaBean area = (AreaBean) session.get(AreaBean.class, ab.getIdArea());
		session.close();
		return area;
	}

	public List<AreaBean> getAreas(int idSucursal) {
		Session session = sf.openSession();
		List<AreaBean> areas = session
				.createQuery("from AreaBean a where a.idArea=?")
				.setInteger(0, idSucursal).list();
		return areas;
	}

	public void merge(AreaBean ab) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.merge(ab);
		session.flush();
		session.getTransaction().commit();
		session.close();
		
	}
}
